package model.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import model.pojo.*;
import model.util.DataHelper;
import controller.util.ControllerHelper;

public class NhaSanXuatDAO 
{
	public static ArrayList<HangSanXuat> HangSanXuatSanPham(String TenLoaiSP){
//	{		
		DataHelper db = new DataHelper();
        if(!db.open())
            return null ;
        String sp = String.format(ControllerHelper.getStoreProcedureString("HangSanXuatLoaiSanPham"),TenLoaiSP);
        ArrayList<HangSanXuat> arr = null ;
        ResultSet rs = db.executeQuery(sp);
        try
        {
        	if(rs.next())
        	{
	        	arr = new ArrayList<HangSanXuat>();
	            
	        	do
	            {
	            	HangSanXuat p = new HangSanXuat();
	                
	                p.setMaHangSX(rs.getString(1));
	                p.setTenHangSX(rs.getString(2));
	                
	                arr.add(p);
	            }while(rs.next());
        	}
        }
        catch(Exception ex){}
        db.close();
        return arr;
	}
	
	public static ArrayList<HangSanXuat> LayDSHangSanXuat()
	{
		ArrayList<HangSanXuat> _arr = new ArrayList<HangSanXuat>();
		
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            ResultSet result = helper.executeProcedure("{call sp_selectAllHangSanXuat()}", para);
            while(result.next())
            {
                HangSanXuat nsx = new HangSanXuat();
                nsx.setMaHangSX(result.getString("MaHangSX"));
                nsx.setTenHangSX(result.getString("TenHangSX"));
                
                _arr.add(nsx);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
  
		
		return _arr;
	}
	
	//them hang san xuat
	public static boolean ThemHangSanXuat(HangSanXuat hsx)
	{
		boolean kq = false;//sp_InsertHangSanXuat
		DataHelper helper = new DataHelper();
		
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(hsx.getTenHangSX());
            para.add(hsx.getMoTaSanPham());
            String sp = ControllerHelper.getStoreProcedureString("ThemHangSX");
            int kqIns = helper.executeInsProcedure(sp, para);
            if(kqIns>0)
            {
            	kq=true;
            }            
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return kq;
	}
	//kiem tra hang san xuat
	public static ArrayList<HangSanXuat> KiemTraHangSanXuat(String tenHang)
	{
		ArrayList<HangSanXuat> arr= new ArrayList<HangSanXuat>();
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(tenHang);
            String sp = ControllerHelper.getStoreProcedureString("KiemTraHangSX");
            ResultSet result = helper.executeProcedure(sp, para);
            while(result.next())
            {
                HangSanXuat nsx = new HangSanXuat();
                nsx.setMaHangSX(result.getString("MaHangSX"));
                nsx.setTenHangSX(result.getString("TenHangSX"));
                
                arr.add(nsx);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return arr;
	}
}
